package cn.mujiankeji.mbrowser.数据.数据库

import androidx.room.ColumnInfo
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Entity
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.PrimaryKey
import androidx.room.Query
import androidx.room.Update
import kotlinx.coroutines.flow.Flow

/**
 * 网站配置数据表
 * 用于存储不同网站的特定配置
 */
@Entity(tableName = "WebsiteConfig")
data class DBWebsiteConfig(
    @PrimaryKey(autoGenerate = true)
    val id: Long = 0,

    @ColumnInfo(name = "host_name")
    val 主机名: String = "",

    @ColumnInfo(name = "ua")
    var UA : String = "",
    
    @ColumnInfo(name = "dark_mode_enabled")
    var 启用暗色模式: Boolean = false,
    
    @ColumnInfo(name = "desktop_mode_enabled")
    var 启用电脑模式: Boolean = false,
    
    @ColumnInfo(name = "incognito_mode_enabled")
    var 启用无痕模式: Boolean = false,
    
    @ColumnInfo(name = "no_image_mode_enabled")
    var 启用无图模式: Boolean = false,
    
    @ColumnInfo(name = "script_enabled")
    var 启用脚本支持: Boolean = true,
    
    @ColumnInfo(name = "user_script_enabled")
    var 启用用户脚本: Boolean = true,
    
    @ColumnInfo(name = "translation_enabled")
    var 启用自动翻译: Boolean = false,

    @ColumnInfo(name = "third_party_cookie_enabled")
    var 启用允许第三方Cookie: Boolean = true,
    
    @ColumnInfo(name = "block_external_app")
    var 禁止唤醒第三方APP: Boolean = false,
    
    @ColumnInfo(name = "ad_block_enabled")
    var 启用广告拦截: Boolean = true,

    @ColumnInfo(name = "content_compliance_enabled")
    var 启用内容合规检测: Boolean = true,


    
    @ColumnInfo(name = "update_time")
    var 更新时间: Long = System.currentTimeMillis()
)

/**
 * 网站配置数据访问对象
 */
@Dao
interface DaoWebsiteConfig {
    
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun 添加或更新配置(配置: DBWebsiteConfig) : Long
    
    @Delete
    suspend fun 删除配置(配置: DBWebsiteConfig)
    
    @Query("SELECT * FROM WebsiteConfig WHERE host_name = :主机名")
    suspend fun 获取配置(主机名: String): DBWebsiteConfig?
    
    @Query("SELECT * FROM WebsiteConfig WHERE host_name = :主机名")
    fun 获取配置Flow(主机名: String): Flow<DBWebsiteConfig?>
    
    @Query("SELECT * FROM WebsiteConfig ORDER BY update_time DESC")
    fun 获取所有配置(): Flow<List<DBWebsiteConfig>>
    
    @Query("DELETE FROM WebsiteConfig WHERE host_name = :主机名")
    suspend fun 按主机名删除配置(主机名: String)
    
    @Update
    suspend fun 更新配置(配置: DBWebsiteConfig)
}